<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="MAC地址" prop="mac">
        <el-input
          v-model="queryParams.mac"
          placeholder="请输入MAC地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备序列号" prop="sn">
        <el-input
          v-model="queryParams.sn"
          placeholder="请输入设备序列号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省份" prop="provinceName">
        <el-input
          v-model="queryParams.provinceName"
          placeholder="请输入省份"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运营商" prop="ispName">
        <!-- <el-input
          v-model="queryParams.ispName"
          placeholder="请输入运营商"
          clearable
          @keyup.enter="handleQuery"
        /> -->
        <el-select v-model="queryParams.ispName" placeholder="请选择运营商" clearable style="width: 195px;">
          <el-option v-for="dict in operator_type" :key="dict.value" :label="dict.label" :value="dict.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="统计日期" prop="recordDate">
        <el-date-picker clearable
          v-model="queryParams.recordDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择统计日期(YYYY-MM-DD)">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['producer:bill:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['producer:bill:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['producer:bill:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['producer:bill:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="billList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed="left"/>
        <el-table-column label="设备MAC地址" align="center" prop="mac" v-if="columns[0].visible" width="280"/>
        <el-table-column label="设备序列号" align="center" prop="sn" v-if="columns[1].visible" width="200"/>
        <el-table-column label="NAT类型" align="center" prop="natType" v-if="columns[5].visible" width="120"/>
        <el-table-column label="UPnP类型" align="center" prop="upnpType" v-if="columns[6].visible" width="100"/>
        <el-table-column label="IPv6状态" align="center" prop="ipv6" v-if="columns[7].visible">
          <template #default="scope">
            <el-switch v-model="scope.row.ipv6" :active-value="1" :inactive-value="0" disabled/>
          </template>
        </el-table-column>
<!--        <el-table-column label="上行带宽(Mbps)" align="center" prop="uploadBandwidth" v-if="columns[8].visible"/>-->
<!--        <el-table-column label="下行带宽(Mbps)" align="center" prop="downloadBandwidth" v-if="columns[9].visible"/>-->
        <el-table-column label="设备收益(元)" align="center" prop="income" v-if="columns[12].visible" width="100"/>
        <el-table-column label="统计日期" align="center" prop="recordDate" width="180" v-if="columns[13].visible">
            <template #default="scope">
            <span>{{ parseTime(scope.row.recordDate, '{y}-{m}-{d}') }}</span>
            </template>
        </el-table-column>
      <el-table-column label="内网ip" align="center" prop="inIp" v-if="columns[10].visible" width="150"/>
      <el-table-column label="外网ip" align="center" prop="outIp" v-if="columns[11].visible" width="150"/>
      <el-table-column label="省份" align="center" prop="provinceName" v-if="columns[2].visible"/>
      <el-table-column label="运营商" align="center" prop="ispName" v-if="columns[3].visible"/>
      <el-table-column label="压测带宽" align="center" prop="stressTest" v-if="columns[4].visible" width="300"/>
        <el-table-column label="备注" align="center" prop="remark" v-if="columns[14].visible"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="160">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['producer:bill:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['producer:bill:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改节点设备平台账单对话框 -->
    <el-dialog :title="title" v-model="open" :width="dialogWidth" append-to-body>
      <el-form ref="billRef" :model="form" :rules="rules">
      <el-row :gutter="20">

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="设备MAC地址" prop="mac">
          <el-input v-model="form.mac" placeholder="请输入设备MAC地址" />
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="设备序列号(唯一标识)" prop="sn">
          <el-input v-model="form.sn" placeholder="请输入设备序列号(唯一标识)" />
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="省份(如: 广东省/北京市)" prop="province">
          <el-input v-model="form.province" placeholder="请输入省份(如: 广东省/北京市)" />
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="运营商(如: 中国移动/电信/联通)" prop="isp">
          <el-input v-model="form.isp" placeholder="请输入运营商(如: 中国移动/电信/联通)" />
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="压测标志: 0-未压测 1-压测中" prop="stressTest">
          <el-input v-model="form.stressTest" placeholder="请输入压测标志: 0-未压测 1-压测中" />
        </el-form-item>
</el-col>



<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="IPv6支持: 0-未开启 1-已启用" prop="ipv6">
          <el-input v-model="form.ipv6" placeholder="请输入IPv6支持: 0-未开启 1-已启用" />
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="上行带宽(Mbps)" prop="uploadBandwidth">
          <el-input v-model="form.uploadBandwidth" placeholder="请输入上行带宽(Mbps)" />
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="下行带宽(Mbps)" prop="downloadBandwidth">
          <el-input v-model="form.downloadBandwidth" placeholder="请输入下行带宽(Mbps)" />
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="内网ip" prop="inIp">
          <el-input v-model="form.inIp" placeholder="请输入内网ip" />
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="外网ip" prop="outIp">
          <el-input v-model="form.outIp" placeholder="请输入外网ip" />
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="设备收益(元)" prop="income">
          <el-input v-model="form.income" placeholder="请输入设备收益(元)" />
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="统计日期(YYYY-MM-DD)" prop="recordDate">
          <el-date-picker clearable
            v-model="form.recordDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择统计日期(YYYY-MM-DD)">
          </el-date-picker>
        </el-form-item>
</el-col>

  <el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        </el-col>
  </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Bill">
import { listBill, getBill, delBill, addBill, updateBill } from "@/api/producer/bill"

const { proxy } = getCurrentInstance()
const { operator_type } = proxy.useDict('operator_type')

const billList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

// 列显隐信息
const columns = ref([
{ key: 0, label: `设备MAC地址（全大写格式）`, visible: true },
{ key: 1, label: `设备序列号(唯一标识)`, visible: true },
{ key: 2, label: `省份(如: 广东省/北京市)`, visible: true },
{ key: 3, label: `运营商(如: 中国移动/电信/联通)`, visible: true },
{ key: 4, label: `压测标志: 0-未压测 1-压测中`, visible: true },
{ key: 5, label: `NAT类型(FullCone/Restricted/Symmetric)`, visible: true },
{ key: 6, label: `UPnP类型(Enabled/Disabled/Unsupported)`, visible: true },
{ key: 7, label: `IPv6支持: 0-未开启 1-已启用`, visible: true },
{ key: 8, label: `上行带宽(Mbps)`, visible: true },
{ key: 9, label: `下行带宽(Mbps)`, visible: true },
{ key: 10, label: `内网ip`, visible: true },
{ key: 11, label: `外网ip`, visible: true },
{ key: 12, label: `设备收益(元)`, visible: true },
{ key: 13, label: `统计日期(YYYY-MM-DD)`, visible: true },
{ key: 14, label: `备注`, visible: true },
])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    mac: null,
    sn: null,
    provinceName: null,
    isp: null,
    stressTest: null,
    natType: null,
    upnpType: null,
    ipv6: null,
    uploadBandwidth: null,
    downloadBandwidth: null,
    inIp: null,
    outIp: null,
    income: null,
    recordDate: null,
  },
  rules: {
    mac: [
      { required: true, message: "设备MAC地址不能为空", trigger: "blur" }
    ],
    sn: [
      { required: true, message: "设备序列号(唯一标识)不能为空", trigger: "blur" }
    ],
    recordDate: [
      { required: true, message: "统计日期(YYYY-MM-DD)不能为空", trigger: "blur" }
    ],
    createTime: [
      { required: true, message: "数据创建时间不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询节点设备平台账单列表 */
function getList() {
  loading.value = true
  listBill(queryParams.value).then(response => {
    billList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    mac: null,
    sn: null,
    province: null,
    isp: null,
    stressTest: null,
    natType: null,
    upnpType: null,
    ipv6: null,
    uploadBandwidth: null,
    downloadBandwidth: null,
    inIp: null,
    outIp: null,
    income: null,
    recordDate: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
  proxy.resetForm("billRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加节点设备平台账单"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getBill(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改节点设备平台账单"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["billRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateBill(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addBill(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除节点设备平台账单编号为"' + _ids + '"的数据项？').then(function() {
    return delBill(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('producer/bill/export', {
    ...queryParams.value
  }, `bill_${new Date().getTime()}.xlsx`)
}

getList()

/** 动态计算字段数 **/
const fieldCount = ref(0)

onMounted(() => {
  // 统计表单字段数量
  fieldCount.value = [
    "mac",
    "sn",
    "province",
    "isp",
    "stressTest",
    "natType",
    "upnpType",
    "ipv6",
    "uploadBandwidth",
    "downloadBandwidth",
    "inIp",
    "outIp",
    "income",
    "recordDate",
    "remark",
  ].length
})

const isTwoColumnForm = computed(() => fieldCount.value > 8)
const dialogWidth = computed(() => isTwoColumnForm.value ? '60vw' : '500px')

</script>
